﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KylinORM.Utils;

namespace KylinORM.CQRS.Publisher
{
    /// <summary>
    /// 为实体更新事件之后提供数据
    /// </summary>
    public class AfterSubmitEvent : AsyncMessage
    {
        /// <summary>
        /// 创建 AfterSubmitEventArgs 的实例
        /// </summary>
        /// <param name="messageName">操作的类型</param>
        /// <param name="affectedRows">受操作影响的行数</param>
        protected internal AfterSubmitEvent(string messageName, int affectedRows)
            : this(messageName, affectedRows, null, null)
        {
        }
        /// <summary>
        /// 创建 AfterSubmitEventArgs 的实例
        /// </summary>
        /// <param name="messageName">操作的类型</param>
        /// <param name="affectedRows">受操作影响的行数</param>
        /// <param name="keys">主键/主键值</param>
        /// <param name="values">非主键/非主键值</param>
        protected internal AfterSubmitEvent(string messageName, int affectedRows, ReadonlyOrderedDictionary keys, ReadonlyOrderedDictionary values)
            : this(messageName, affectedRows, keys, values, null)
        {
        }
        /// <summary>
        /// 创建 AfterSubmitEventArgs 的实例
        /// </summary>
        /// <param name="messageName">操作的类型</param>
        /// <param name="affectedRows">受操作影响的行数</param>
        /// <param name="keys">主键/主键值</param>
        /// <param name="values">非主键/非主键值</param>
        /// <param name="exception">获取更新操作过程中引发的异常（如果有）</param>
        protected internal AfterSubmitEvent(string messageName, int affectedRows, ReadonlyOrderedDictionary keys, ReadonlyOrderedDictionary values,
            Exception exception)
        {
            this.MessageName = messageName;
            this.AffectedRows = affectedRows;
            this.PrimaryFields = keys ?? new ReadonlyOrderedDictionary(new List<KeyValuePair<string, object>>());
            this.EntityFields = values ?? new ReadonlyOrderedDictionary(new List<KeyValuePair<string, object>>());
            this.Exception = exception;
            this.ExceptionHandled = (exception != null);
        }
        /// <summary>
        /// 受操作影响的行数
        /// </summary>
        public int AffectedRows
        {
            get;
            private set;
        }
        /// <summary>
        /// 获取更新操作过程中引发的异常（如果有）
        /// </summary>
        public Exception Exception
        {
            get;
            private set;
        }
        /// <summary>
        /// 指示是否在事件处理程序中处理了操作过程中引发的异常
        /// </summary>
        public bool ExceptionHandled
        {
            get;
            private set;
        }
        /// <summary>
        /// 获取操作实体的主键字段
        /// </summary>
        public ReadonlyOrderedDictionary PrimaryFields
        {
            get;
            private set;
        }
        /// <summary>
        /// 获取操作实体的全部字段
        /// </summary>
        public ReadonlyOrderedDictionary EntityFields
        {
            get;
            private set;
        }
    }
}
